package  common.db.id;


import common.db.RowProcessor;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;

import cz.atria.common.lang.Util;

/**
 * User: Kostylev Alexey
 * Date: 11.11.2008
 * Time: 10:52:03
 */
public abstract class IdObjectCachedProcessor<E extends IdObject> implements RowProcessor<E>
{
	private static Map<Class, Map<Integer, IdObject>> objects = new HashMap<Class, Map<Integer, IdObject>>();

	protected synchronized void addObject(IdObject object)
	{
		Map<Integer, IdObject> map = objects.get(object.getClass());
		if (Util.isEmpty(map))
		{
			map = new HashMap<Integer, IdObject>();
			objects.put(object.getClass(), map);
		}

		map.put(object.getId(), object);
	}

	protected synchronized E findObject(Integer id, Class<E> clazz)
	{
		Map<Integer, IdObject> map = objects.get(clazz);
		if (Util.isEmpty(map))
		{
			map = new HashMap<Integer, IdObject>();
			objects.put(clazz, map);
		}

		E object = (E)map.get(id);
		return object;
	}
}
